<?php
include_once ("../Login/databaseconnection.php");
 $Training_Name=$_POST['Training_Name'];
	   	  $resultnet=mysql_query("SELECT * FROM training WHERE Training_Name='$Training_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Training_Id=$rownet['Training_Id'];
}
	  
	  $TeaName=$_POST['TeaName'];
$name=explode(" ",$TeaName);
$fn=$name[0];
$ln=$name[1];
$results= mysql_query("SELECT * FROM teacher Where First_Name='$fn' AND Last_Name='$ln'");
if($row = mysql_fetch_array($results))
{
 $T_NIC_Number=$row['T_NIC_Number'];
}
      if($_SESSION['Access_Type']=="Administrator")
   {
    
	if( $Training_Id!="Select One"&&$T_NIC_Number!="Select One")
	   {
		 $result=mysql_query("SELECT * FROM resource_team WHERE Training_Id='$Training_Id' AND T_NIC_Number='$T_NIC_Number'");
		 	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
		 $Training_Id=$row['Training_Id'];
   $T_NIC_Number=$row['T_NIC_Number'];
 $Member_Id=$row['Member_Id'];
    $From_Date=$row['From_Date'];
		 }
	    if(mysql_num_rows($result)==0)
   {

	   $_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
   else
   {
   	  $del=mysql_query("DELETE FROM resource_team WHERE Training_Id='$Training_Id' AND T_NIC_Number='$T_NIC_Number'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }

  if(mysql_affected_rows()==0)
  {
  $Training_Id='';
   $T_NIC_Number='';
 $Member_Id='';
    $From_Date='';
 $_SESSION['error']="Sorry unable to delete this record!";
  }
  }
   } 
   }
   }
    else if($_SESSION['Access_Type']=="Teacher in Charge")
   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  $get_Sch1=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$T_NIC_Number'");
   if($rowsch1=mysql_fetch_array($get_Sch1))
{
  $School_Id=$rowsch1['School_Id'];

   if($School_Id==$Own_School_Id)
	{
	if( $Training_Id!="Select One"&&$T_NIC_Number!="Select One")
	   {
		 $result=mysql_query("SELECT * FROM resource_team WHERE Training_Id='$Training_Id' AND T_NIC_Number='$T_NIC_Number'");
		 	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
		 $Training_Id=$row['Training_Id'];
   $T_NIC_Number=$row['T_NIC_Number'];
 $Member_Id=$row['Member_Id'];
    $From_Date=$row['From_Date'];
		 }
	    if(mysql_num_rows($result)==0)
   {

	   $_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
   else
   {
   $Accessed_By=$_SESSION['NIC_Number'];
   date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
   if($row=mysql_fetch_array($result))
   {
   $Training_Id=$rowrt['Training_Id'];
   $T_NIC_Number=$rowrt['T_NIC_Number'];
   $Member_Id=$rowrt['Member_Id'];
   $From_Date=$rowrt['From_Date'];
   $Last_Updated_Date=$row['Last_Updated_Date'];  
   }
   	 $sql=mysql_query("INSERT INTO deleted_resource_team(Training_Id,T_NIC_Number,Member_Id,From_Date,Deleted_date,Accessed_By,Last_Updated_Date) VALUES('$Training_Id','$T_NIC_Number','$Member_Id','$From_Date','$t_d','$Accessed_By','$Last_Updated_Date')");
if($sql){
   	  $del=mysql_query("DELETE FROM resource_team WHERE Training_Id='$Training_Id' AND T_NIC_Number='$T_NIC_Number'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }
  if(mysql_affected_rows()==0)
  {
   $Training_Id='';
   $T_NIC_Number='';
 $Member_Id='';
    $From_Date='';
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_resource_team WHERE Training_Id='$Training_Id' AND Deleted_Date='$t_d' AND T_NIC_Number='$T_NIC_Number'");
  }
  }
     if(!$sql)
{
$Training_Id='';
   $T_NIC_Number='';
 $Member_Id='';
    $From_Date='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
  }
  }
  }
  }
   else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
  }
  }
  else
   {
   $_SESSION['errornotallo']="This person's record is not registered into Teacher's record!";
   }
   }
    else
  {
  $_SESSION['errornot2']="Sorry you are not an authorised user!";
  }
  }
   if( $Training_Id=="Select One"||$T_NIC_Number=="Select One")
	   {
	   $_SESSION['errorin']="Please select the Training Id and Teacher's NIC Number!";
		 }
 ?>
